package org.tinaelle.client.utils;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Ext;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.PanelListenerAdapter;
import com.gwtext.client.widgets.event.WindowListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;

/**
 * This is the Top class for all panel displayed in the center area of the application.
 * 
 * It provides some features like :
 * <ul>
 * <li>Help Window (override the 'public String getHelpText()' to get 'help' button displayed in the toolbar)</li>
 * <li>Introduction Panel on the left (override the 'public String getIntro() to get the panel displayed at the right)</li>
 * <li>...</li>
 * </ul>
 * 
 * @author Alexandre Clavaud
 * @version $Revision$
 * 
 */
public abstract class TinaelCenterPanel extends Panel {
	private Window	      helpWindow	= null;
	private final String	helpIconCls	= "source-icon";
	private ToolbarButton	helpButton;
	private Toolbar	      toolbar;
	protected Panel	      panel;

	protected TinaelCenterPanel() {
		setTitle( getTitle() );
		setClosable( true );
		setTopToolbar( new Toolbar() );
		setPaddings( 20 );
		setLayout( new FitLayout() );
		setBorder( false );
		setAutoScroll( true );
		addListener( new PanelListenerAdapter() {
			@Override
			public void onActivate(final Panel panel) {
				TinaelCenterPanel.this.onActivate();
			} // end onActivate()
		} // end new
		);
	} // end OnelineJobCenterPanel()

	protected void onActivate() {
		final Panel viewPanel = getViewPanel();

		if ( viewPanel instanceof Window ) {
			( ( Window ) viewPanel ).show();
		} // end if
	} // end onActivate()

	/**
	 * After the Panel has been rendered, add the Help button and toolbar if needed.
	 */
	@Override
	protected void afterRender() {
		final ButtonListenerAdapter listener = new ButtonListenerAdapter() {
			@Override
			public void onClick(final Button button, final EventObject e) {
				final Window window = getHelpWindow();
				window.show( helpButton.getId() );
			} // end onClick()
		} // end new
		;

		helpButton = new ToolbarButton( "View Help", listener );
		helpButton.setIconCls( helpIconCls );

		if ( getHelpText() == null ) {
			helpButton.setDisabled( true );
		} // end if

		toolbar = getTopToolbar();

		toolbar.addFill();
		toolbar.addButton( helpButton );

		addViewPanel();
	} // end afterRender()

	/**
	 * Generate the View Panel to display in the center This function will generate an Intro Panel if the method 'public
	 * String getIntro()' is overridden.
	 */
	private void addViewPanel() {
		final Panel mainPanel = new Panel();
		mainPanel.setBorder( false );
		// mainPanel.setLayout(new BorderLayout());
		mainPanel.setLayout( new BorderLayout() );

		Panel viewPanel = getViewPanel();

		if ( viewPanel instanceof Window ) {
			viewPanel.show();
			viewPanel = new Panel();
		} // end if

		viewPanel.setAutoScroll( true );
		viewPanel.setBorder( false );

		final BorderLayoutData centerLayoutData = new BorderLayoutData( RegionPosition.CENTER );
		// centerLayoutData.setMargins(new Margins(15, 15, 15, 15));
		mainPanel.add( viewPanel, centerLayoutData );

		final boolean hasIntro = getIntro() != null;

		if ( hasIntro ) {
			final Panel rightPanel = new Panel();
			rightPanel.setCollapsible( true );
			rightPanel.setFrame( true );
			rightPanel.setPaddings( 0, 15, 0, 0 );
			rightPanel.setWidth( 210 );
			rightPanel.setLayout( new VerticalLayout( 15 ) );

			final Panel introPanel = new Panel();
			introPanel.setIconCls( "book-icon" );
			introPanel.setFrame( true );
			introPanel.setTitle( "Overview" );
			introPanel.setHtml( "<p>" + getIntro() + "</p>" );

			// introPanel.setPaddings(0, 0, 0, 15);
			rightPanel.add( introPanel );

			final BorderLayoutData eastLayoutData = new BorderLayoutData( RegionPosition.EAST );
			eastLayoutData.setSplit( true );
			eastLayoutData.setMinSize( 175 );
			eastLayoutData.setMaxSize( 400 );
			eastLayoutData.setMargins( new Margins( 0, 0, 5, 0 ) );

			mainPanel.add( rightPanel, eastLayoutData );
		} // end if

		add( mainPanel );
	} // end addViewPanel()

	/**
	 * Generate the Help Window, used only if 'public String getHelpText()' has been overridden
	 * 
	 * @return The Help Window
	 */
	private Window getHelpWindow() {
		if ( helpWindow == null ) {
			helpWindow = new Window();
			helpWindow.setTitle( "Help" );
			helpWindow.setLayout( new FitLayout() );
			helpWindow.setIconCls( helpIconCls );

			int width = 640;
			final int userWidth = Ext.getBody().getWidth( true ) - 20;

			if ( userWidth < width ) {
				width = userWidth;
			} // end if

			helpWindow.setWidth( width );

			int height = 600;
			final int userHeight = Ext.getBody().getHeight( true ) - 96;

			if ( userHeight < height ) {
				height = userHeight;
			} // end if

			helpWindow.setHeight( height );

			helpWindow.setAnimateTarget( helpButton.getId() );

			final int[] panelXY = getPosition();

			final int windowTop = panelXY[ 1 ] + toolbar.getEl().getHeight( true ) + 20;
			final int windowLeft = Ext.getBody().getWidth( true ) - ( helpWindow.getWidth() + 20 );
			helpWindow.setPosition( windowLeft, windowTop );
			helpWindow.addListener( new WindowListenerAdapter() {
				@Override
				public void onClose(final Panel panel) {
					helpWindow = null;
				} // end onClose()
			} // end new
			        );

			final TabPanel sourceTabPanel = new TabPanel();
			sourceTabPanel.setActiveTab( 0 );

			final String helpText = getHelpText();

			if ( helpText != null ) {
				final Panel sourceTab = new Panel( "Java" );
				sourceTab.setLayout( new FitLayout() );
				sourceTab.setIconCls( "java-icon" );
				sourceTab.setHtml( helpText );
				sourceTabPanel.add( sourceTab );
			} // end if

			helpWindow.add( sourceTabPanel );
		} // end if

		return helpWindow;
	} // end getHelpWindow()

	/**
	 * If overridden, the a help button is display on the toolbar
	 * 
	 * @return The help text in HTML
	 */
	public String getHelpText() {
		return null;
	} // end getHelpText()

	/**
	 * If overridden, the a help button is display on the toolbar
	 * 
	 * @return The help text in HTML
	 */
	public String getIntro() {
		return null;
	} // end getIntro()

	/**
	 * This is the main function to implements, it contains the Panel to display on the center of the screen
	 * 
	 * @return The panel to display
	 */
	public abstract Panel getViewPanel();
} // end OnelineJobCenterPanel
